from google.appengine.api import users
from google.appengine.ext import webapp
from google.appengine.ext.webapp import util, template


class MainHandler(webapp.RequestHandler):
    def get(self):
        if users.get_current_user():
            self.response.out.write(template.render("templates/site.html", {
                "log_out": users.create_logout_url("/")
            }))
        else:
            self.redirect(users.create_login_url("/"))


class SyncHandler(webapp.RedirectHandler):
    def get(self):
        pass

    def post(self):
        pass

            
def real_main():
    application = webapp.WSGIApplication([
        (r"/", MainHandler),
        (r"/sync", SyncHandler),
    ], debug=True)
    util.run_wsgi_app(application)


def profile_main():
    # This is the main function for profiling
    # We've renamed our original main() above to real_main()
    import cProfile, pstats
    prof = cProfile.Profile()
    prof = prof.runctx("real_main()", globals(), locals())
    print("<pre>")
    stats = pstats.Stats(prof)
    stats.sort_stats("time")  # Or cumulative
    stats.print_stats(720)  # how many to print
    # The rest is optional.
    # stats.print_callees()
    # stats.print_callers()
    print("</pre>")


main = real_main


if __name__ == '__main__':
    main()
